<template>
  <div class="not-found">
    <el-result
      icon="warning"
      title="404"
      sub-title="抱歉，您访问的页面不存在"
    >
      <template #extra>
        <div class="actions">
          <el-button type="primary" @click="router.push('/')">
            返回首页
          </el-button>
          <el-button @click="router.back()">
            返回上一页
          </el-button>
        </div>
        <div class="tips">
          <p>您可以尝试以下操作：</p>
          <ul>
            <li>检查输入的网址是否正确</li>
            <li>清除浏览器缓存后重试</li>
            <li>返回首页浏览其他内容</li>
          </ul>
        </div>
      </template>
    </el-result>

    <!-- 推荐阅读 -->
    <el-card class="recommended" v-if="recommendedBlogs.length">
      <template #header>
        <div class="header">
          <span>推荐阅读</span>
        </div>
      </template>
      
      <div class="blog-list">
        <div
          v-for="blog in recommendedBlogs"
          :key="blog.id"
          class="blog-item"
          @click="router.push(`/blog/${blog.id}`)"
        >
          <el-image
            v-if="blog.cover"
            :src="blog.cover"
            fit="cover"
            class="cover"
          />
          <div class="info">
            <h3 class="title">{{ blog.title }}</h3>
            <p class="summary">{{ blog.summary }}</p>
            <div class="meta">
              <span class="time">
                <el-icon><Calendar /></el-icon>
                {{ formatDate(blog.createTime) }}
              </span>
              <span class="views">
                <el-icon><View /></el-icon>
                {{ formatNumber(blog.viewCount) }} 阅读
              </span>
            </div>
          </div>
        </div>
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { Calendar, View } from '@element-plus/icons-vue'
import { useBlogStore } from '@/stores/blog'
import type { Blog } from '@/types'
import { formatDate, formatNumber } from '@/utils/format'

const router = useRouter()
const blogStore = useBlogStore()

const recommendedBlogs = ref<Blog[]>([])

// 获取推荐文章
const fetchRecommendedBlogs = async () => {
  try {
    const blogs = await blogStore.fetchRecommendedBlogs()
    recommendedBlogs.value = blogs.slice(0, 3) // 只显示前3篇
  } catch (error) {
    console.error('Failed to fetch recommended blogs:', error)
  }
}

onMounted(() => {
  fetchRecommendedBlogs()
})
</script>

<style lang="scss" scoped>
.not-found {
  min-height: 100vh;
  padding: 40px 20px;
  background-color: #f5f5f5;
  
  :deep(.el-result) {
    background-color: #fff;
    border-radius: 8px;
    padding: 48px 32px;
    box-shadow: 0 2px 12px 0 rgba(0,0,0,0.1);
    
    .el-result__icon {
      svg {
        width: 120px;
        height: 120px;
      }
    }
    
    .el-result__title {
      font-size: 72px;
      color: #303133;
      margin-top: 20px;
    }
    
    .el-result__sub-title {
      font-size: 20px;
      color: #606266;
      margin-top: 10px;
    }
  }
  
  .actions {
    margin-bottom: 24px;
    
    .el-button {
      min-width: 120px;
      margin: 0 10px;
    }
  }
  
  .tips {
    text-align: left;
    color: #606266;
    
    p {
      margin-bottom: 10px;
    }
    
    ul {
      padding-left: 20px;
      
      li {
        line-height: 1.8;
      }
    }
  }
  
  .recommended {
    max-width: 800px;
    margin: 40px auto 0;
    
    .header {
      font-size: 18px;
      font-weight: bold;
    }
    
    .blog-list {
      .blog-item {
        display: flex;
        padding: 20px 0;
        cursor: pointer;
        transition: all 0.3s;
        
        &:not(:last-child) {
          border-bottom: 1px solid #eee;
        }
        
        &:hover {
          transform: translateY(-2px);
          
          .title {
            color: $primary-color;
          }
        }
        
        .cover {
          width: 200px;
          height: 120px;
          border-radius: 4px;
          margin-right: 20px;
          flex-shrink: 0;
        }
        
        .info {
          flex: 1;
          overflow: hidden;
          
          .title {
            margin: 0 0 10px;
            font-size: 18px;
            color: $text-primary;
            @include text-overflow;
          }
          
          .summary {
            margin: 0 0 10px;
            color: $text-regular;
            font-size: 14px;
            line-height: 1.6;
            @include multi-line-overflow(2);
          }
          
          .meta {
            color: $text-secondary;
            font-size: 13px;
            
            span {
              display: inline-flex;
              align-items: center;
              margin-right: 20px;
              
              .el-icon {
                margin-right: 4px;
              }
            }
          }
        }
      }
    }
  }
}

// 响应式布局
@media (max-width: $breakpoint-sm) {
  .not-found {
    padding: 20px 10px;
    
    :deep(.el-result) {
      padding: 32px 16px;
      
      .el-result__icon {
        svg {
          width: 80px;
          height: 80px;
        }
      }
      
      .el-result__title {
        font-size: 48px;
      }
      
      .el-result__sub-title {
        font-size: 16px;
      }
    }
    
    .recommended {
      .blog-list {
        .blog-item {
          flex-direction: column;
          
          .cover {
            width: 100%;
            height: 180px;
            margin: 0 0 15px;
          }
        }
      }
    }
  }
}
</style> 